/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solucionessgh.Paises;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;
/**
 *
 * @author roderick
 */
public class PaisesModel extends DataBase{

    private int id;
    private String paidesc;
    private String paidescm;
    private String paiabr;
    private String painum;

    public PaisesModel(){

    }

    private boolean validar(){
        return true;
    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("INSERT INTO paises(paidesc, paidescm, paiabr, painum) VALUES ( ?, ?, ?, ?)");
                stmt.setString(1,this.paidesc);
                stmt.setString(2,this.paidescm);
                stmt.setString(3,this.paiabr);
                stmt.setString(4,this.painum);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("UPDATE paises SET paidesc=?, paidescm=?, paiabr=?, painum=? where paicod = ? ;");
                stmt.setString(1,this.paidesc);
                stmt.setString(2,this.paidescm);
                stmt.setString(3,this.paiabr);
                stmt.setString(4,this.painum);
                stmt.setInt(5, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public boolean delete(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from paises where paicod=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT paicod, paidesc, paidescm, paiabr, painum FROM paises order by paidesc ");
    }


    public ResultSet finder(String var){
        return this.find("SELECT paicod, paidesc, paidescm, paiabr, painum FROM paises WHERE paidesc LIKE '"+var+"%';");
    }

    public ResultSet finder(int var){
        return this.find("SELECT paicod, paidesc, paidescm, paiabr, painum FROM paises WHERE paicod = "+var+";");
    }



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public String getPaiabr() {
        return paiabr;
    }

    public void setPaiabr(String paiabr) {
        this.paiabr = paiabr;
    }

    public String getPaidesc() {
        return paidesc;
    }

    public void setPaidesc(String paidesc) {
        this.paidesc = paidesc;
    }

    public String getPaidescm() {
        return paidescm;
    }

    public void setPaidescm(String paidescm) {
        this.paidescm = paidescm;
    }

    public String getPainum() {
        return painum;
    }

    public void setPainum(String painum) {
        this.painum = painum;
    }
}
